<template>
	<view>
		<cu-custom style="height: 0px !important;" bgColor="bg-gradual-blue" :isBack="true">
			 <block slot="content">签到</block>
		</cu-custom>
		<view  class="content">
			<active-form style="width:100% !important" :formDate.sync="formData" num></active-form>
			<view class="subform" @click="sub">提交</view>
		</view>
	</view>
</template>

<script>
import ActiveForm from "@/components/active-form/active-form";
import request from '@/common/request.js';
export default {
  components: {
    ActiveForm,
  },
  data() {
    return {
		qrcodeList:[],
		getTopicParam:{
			JobId:'',
		},
		requestData:{
			JobId:'',
			QrcodeId:'',
			SignId:'',
			Result:'',
			Longitude:'',
			Latitude:''
		},
		formDataBase: [
			
		],
        formData: [],
    };
  },
  onLoad(option){
	this.requestData.QrcodeId=option.qrcode_id
	this.getTopicList()  
  },
  onShow: function() {
	this.formDataBase[1].url=this.$request22.baseUrl
  	this.getSetting()
  },
  methods: {
	getTopicList(){
		let that=this
		uni.showLoading({title: '加载中'})
		that.getTopicParam.JobId=resStorage.data.id;
		let opts = {url: 'api/GetJobTopicList',method: 'get'};
		request.httpTokenRequest(opts,that.getTopicParam).then(res => {
			if(res.data.code == 1){
				let fromData=[]
				let result=res.data.data;
				
				let list=[]
				for(let i=0;i<result.length;i++){
					list=[]
					let item=result[i]
					//1 单选，2 多选，3数字，4文字
					if(item.TopicType==1||item.TopicType==2){
						let fromItem={}
						let rules={}
						for(let j=0;j<item.TopicOptionArry.length;j++){
							let listItem={}
							if(item.TopicType==1){
								listItem.value=item.TopicOptionArry[j]
								listItem.label=item.TopicOptionArry[j]
							}else{
								listItem.value=item.TopicOptionArry[j]
								listItem.name=item.TopicOptionArry[j]
							}
							list.push(listItem)
						}
						fromItem.id=item.Id;
						fromItem.placeholder=item.TopicTips;
						fromItem.label=item.TopicTitle+'：';
						fromItem.type=item.TopicType==1?'radio':'checkbox';
						fromItem.list=list;
						rules.name='result'+i;
						rules.value='';
						rules.label='';
						rules.verify=item.Required==1?true:false;
						rules.errMess='';
						fromItem.rules=rules;
						fromData.push(fromItem)
					}else if(item.TopicType==3){
						let fromItem={}
						fromItem.id=item.Id;
						fromItem.placeholder=item.TopicTips+'('+item.TopicUnit+')';
						fromItem.label=item.TopicTitle+'：';
						fromItem.type='text';
						let rules={}
						rules.name='result'+i;
						rules.value='';
						rules.label='';
						rules.verify=item.Required==1?true:false;
						rules.errMess='';
						fromItem.rules=rules;
						fromData.push(fromItem)
					}else{
						let fromItem={}
						fromItem.id=item.Id;
						fromItem.placeholder=item.TopicTips;
						fromItem.label=item.TopicTitle+'：';
						fromItem.type='textarea';
						let rules={}
						rules.name='result'+i;
						rules.value='';
						rules.label='';
						rules.verify=item.Required==1?true:false;
						rules.errMess='';
						fromItem.rules=rules;
						fromData.push(fromItem)
					}
				}
				that.formData=[]
				that.formData=fromData
				that.formData.push(that.formDataBase[0])
				that.formData.push(that.formDataBase[1])
				
				
				uni.hideLoading();
			}
			else{
				uni.hideLoading();
				uni.showToast({ title: res.data.msg, icon: 'none' });
			}
		})
		
	},
    // 提交表单
    sub() {
		this.$vervify(this.formData); //表单校验 成功会继续往下走 失败抛出异常
		const res = this.$submitForm(this.formData); //校验成功 获取表单值
		
		uni.showLoading({
			title: '提交中'
		})
		
		let that=this
		console.log(JSON.stringify(that.formData))
		uni.getStorage({
			key: 'JobId',
			success: function (resJob) {
				that.requestData.JobId=resJob.data.id;
				//获取位置信息
				uni.getLocation({
					type: 'gcj02',
					altitude: true,
					success(resLocation) {
						that.requestData.Longitude=resLocation.longitude;
						that.requestData.Latitude=resLocation.latitude;
						let opts = {
							url: 'api/AnswerQrcodeTopic',
							method: 'post'
						};
						that.requestData.Result=JSON.stringify(that.formData)
						request.httpTokenRequest(opts,that.requestData).then(res => {
						if(res.data.code == 1){
							uni.hideLoading();
							uni.showToast({ title:'添加成功', icon: '/static/success.png' });
							uni.getStorage({
								key: 'PageCur',
								success: function (res1) {
									setTimeout(() => {
										 uni.reLaunch({
											url: '/pages/index/tabbar?PageCur='+res1.data
										 });
									}, 0)
								},fail() {
									setTimeout(() => {
										 uni.reLaunch({
										 	url: '/pages/index/tabbar?PageCur=search'
										 });
									}, 0)
								}
							});
							
							
						}
						else{
							uni.hideLoading();
							uni.showToast({ title: res.data.msg, icon: 'none' });
						}
					})
					},fail() {
						uni.showToast({ title: '请重试', icon: 'none' });
					}
				})
			},fail() {
				uni.showToast({ title: '请重试', icon: 'none' });
			}
		});
    },
	getSetting() {
		const that = this
		uni.getSetting({
		  success(res) {
			 console.log(res)
				if (res.authSetting['scope.userLocation']) {
					that.getLocation()
				} else {
					that.getAuthorize()
				}
		  }
		})
	},
	//  获取用户的地理位置，
	getLocation() {
		const that = this
		uni.getLocation({
			type: 'gcj02',
			altitude: true,
			success(res) {
				that.requestData.Longitude=res.longitude;
				that.requestData.Latitude=res.latitude;
			}
		})
	}
  },
};
</script>

<style lang="less" scoped>
.form-item .flex-row--c{
	width: 100%;
}
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .subform {
	left: 5%;
	bottom: 8%;
	width: 90%;
	text-align: center;
    padding: 20rpx 60rpx;
	margin-bottom: 40rpx;
    border-radius: 18rpx;
    background-color: #0081ff;
	color: white;
  }
}
</style>